package com.vertxsample.samples.testconsulclient

import io.vertx.core.DeploymentOptions
import io.vertx.core.Vertx
import io.vertx.core.json.JsonObject
import io.vertx.ext.consul.ConsulClient
import io.vertx.ext.consul.ConsulClientOptions
import org.slf4j.LoggerFactory


class App {

    companion object {
        val logger = LoggerFactory.getLogger(this::class.java)
        @JvmStatic
        fun main(args: Array<String>) {
            val vertx = Vertx.vertx()

            val consul_host =System.getProperty("consul.host") //consul地址
            val consul_port=Integer.parseInt( System.getProperty("consul.port"))  //consul 端口
            val consulClientOptions = ConsulClientOptions(  )
            consulClientOptions.host=consul_host
            consulClientOptions.port=consul_port
            var consulClient = ConsulClient.create(vertx, consulClientOptions)

            //获取配置
            consulClient.getValue("config/application,test/data", { res ->

                if (res.succeeded()) {
                    try {
                        val options= JsonObject(res.result().value)

                        //线程池配置
                        var deployMentOptions =DeploymentOptions().setWorker(true)
                            .setWorkerPoolName(options.getString("vertx.workerPoolName"))
                            .setWorkerPoolSize(options.getInteger("vertx.workerPoolSize"))

                        //部署web服务
                        vertx.deployVerticle(StarterVerticle(options),deployMentOptions)
                    }catch (e :Exception){
                        logger.error("",e)
                    }

                } else {
                    logger.error("", res.cause())
                }
            })


        }
    }
}